
PLUG.Platform = PLUG_PLATFORM_CLIENT

PLUG.ClassName = "cl_submenu"
PLUG.PrintName = "Clientside Sub-Menus"
PLUG.Author = "thomasfn"

PLUG.Hard = true

local SELGRP, SELWIN, SELGM, SELMAP, SELAC, SELENTTYPE, SELENTCLASS

function PLUG:BuildSubMenu( classname, window )
	if (classname == "playermenu") then
		window:SetTitle( "Player Management" )
		local pn = vgui.Create( "DComboBox", window )
		pn:SetPos( 5, 30 )
		pn:SetSize( window:GetWide()*0.2, window:GetTall()-35 )
		pn:SetMultiple( false )
		for _, pl in pairs( player.GetAll() ) do
			local item = pn:AddItem( pl:Nick() )
			item._PLAYER = pl
		end
		pn.SelectItem2 = pn.SelectItem
		function pn:SelectItem( item, onlyme )
			local ply = item._PLAYER
			if ((ply) && (ply:IsValid())) then
				plugs.Call( "BuildPlayerPanel", window, ply )
			end
			self:SelectItem2( item, onlyme )
		end
	end
	if (classname == "playercpanel") then
		local men_w = window:GetWide()
		plugs.Call( "AddPlayerOpList", window, "Basic Actions", "BASIC", men_w * 0.38, men_w*0.2 )
		plugs.Call( "AddPlayerOpList", window, "Punishment", "PUNISH", men_w * 0.58, men_w*0.2 )
		plugs.Call( "AddPlayerOpList", window, "Misc Actions", "MISC", men_w * 0.78, men_w*0.2 )
		plugs.Call( "CreateLabel2", "Name: " .. window._PLAYER:Nick(), 10, 10, window )
		plugs.Call( "CreateLabel2", "Usergroup: " .. window._PLAYER:Usergroup(), 10, 30, window )
		
		local img = vgui.Create( "DPanel", window )
		img:SetPos( 10, 80 )
		img:SetSize( men_w * 0.4 - 20, window:GetTall() - 90 )
		local bgcol = Color( 50, 50, 50 )
		function img:Paint()
			draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), bgcol )
			draw.SimpleText( "Time Spent Playing", "ScoreboardText", self:GetWide()*0.5, 5, color_white, 1, TEXT_ALIGN_TOP )
			draw.SimpleText( "(Resets when auto promoted)", "ScoreboardText", self:GetWide()*0.5, 25, color_white, 1, TEXT_ALIGN_TOP )
			local time = plugs.Call( "ToHoursMins", window._PLAYER:GetNWInt( "gman_timer" ) )
			draw.SimpleText( time, "ScoreboardText", self:GetWide()*0.5, 45, color_white, 1, TEXT_ALIGN_TOP )
			
			local tid = window._PLAYER:Team()
			draw.SimpleText( "Team", "ScoreboardText", self:GetWide()*0.5, 85, color_white, 1, TEXT_ALIGN_TOP )
			draw.SimpleText( team.GetName( tid ), "ScoreboardText", self:GetWide()*0.5, 105, team.GetColor( tid ), 1, TEXT_ALIGN_TOP )
		end
		
		local btn = vgui.Create( "DButton", window )
		btn:SetText( "Refresh privileges" )
		btn:SetSize( men_w * 0.2, 22 )
		btn:SetPos( 10, 50 )
		function btn:DoClick()
			plugs.Call( "SendCommand", "getprivs", window._PLAYER:GetGID() )
		end
	end
	if (classname == "groupmenu") then
		window:SetTitle( "Group Management" )
		local pn = vgui.Create( "DComboBox", window )
		pn:SetPos( 5, 30 )
		pn:SetSize( window:GetWide()*0.2, window:GetTall()-35 )
		pn:SetMultiple( false )
		local item = pn:AddItem( "[ Add New ]" )
		item._UGNAME = ""
		item._ADDNEW = true
		local item = pn:AddItem( "[ Refresh ]" )
		item._UGNAME = ""
		item._REFRESH = true
		for _, ug in pairs( plugs.Call( "GetUGSNames" ) ) do
			local item = pn:AddItem( ug[2] )
			item._UGNAME = ug[1]
		end
		pn.SelectItem2 = pn.SelectItem
		function pn:SelectItem( item, onlyme )
			if (item._ADDNEW) then
				self:SelectItem2( item, onlyme )
				SELWIN = nil
				SELGRP = nil
				plugs.Call( "BuildNewGroupPanel", window )
				return
			end
			if (item._REFRESH) then
				self:SelectItem2( item, onlyme )
				plugs.Call( "SendCommand", "getgrps" )
				return
			end
			local ug = item._UGNAME
			if (ug) then
				SELGRP = ug
				SELWIN = window
				plugs.Call( "BuildGroupPanel", window, ug )
			end
			self:SelectItem2( item, onlyme )
		end
		window._COMBOBOX = pn
	end
	if (classname == "groupnewpanel") then
		local w = window:GetWide()
		local h = window:GetTall()
		plugs.Call( "CreateLabel2", "Create new Usergroup", 10, 10, window )
		local tpos = 15 + (plugs.Call( "CreateLabel2", "Name:", 10, 30, window ):GetWide())
		plugs.Call( "CreateLabel2", "The name is the title of the group. It can contain spaces, capital letters and numbers but nothing else.", 10, 50, window )
		local txta = vgui.Create( "DTextEntry", window )
		txta:SetPos( tpos, 30 )
		txta:SetSize( w*0.4, 20 )
		txta:SetValue( "Untitled" )
		tpos = 15 + (plugs.Call( "CreateLabel2", "Base Usergroup:", 10, 70, window ):GetWide())
		local txtb = vgui.Create( "DTextEntry", window )
		txtb:SetPos( tpos, 70 )
		txtb:SetSize( w*0.4, 20 )
		txtb:SetValue( plugs.Call( "GetDefaultGroup" ) or "restricted" )
		plugs.Call( "CreateLabel2", "The base usergroup is the group to copy all settings from. You can change the settings afterwards.", 10, 90, window )
		local btn = vgui.Create( "DImageButton", window )
		btn:SetPos( 10, 110 )
		btn:SetSize( 64, 64 )
		btn:SetMaterial( "gmanage/ico_add" )
		btn._GRPNAME = txta
		btn._GRPBASE = txtb
		btn._ERR = plugs.Call( "CreateLabel2", "", 10, 180, window )
		function btn:DoErr( err )
			self._ERR:SetText( err )
			self._ERR:SizeToContents()
		end
		function btn:DoClick()
			local nm = self._GRPNAME:GetValue()
			if (!nm) then return self:DoErr( "Please enter a name." ) end
			if (nm == "") then return self:DoErr( "Please enter a name." ) end
			if (type( nm ) != "string") then return self:DoErr( "Please enter a valid name." )  end
			if (string.len( nm ) < 2) then return self:DoErr( "Name is too short!" ) end
			if (string.len( nm ) > 16) then return self:DoErr( "Name is too long!" ) end
			local bs = self._GRPBASE:GetValue()
			if (bs == "") then bs = "none" end
			plugs.Call( "SendCommand", "grp_new", bs, nm )
			self:DoErr( "Usergroup details submitted." )
		end
	end
	if (classname == "groupcpanel") then
		// Get the window size
		local w, h = window:GetSize()
		
		// Get the usergroup
		local grp = plugs.Call( "GetUG", window._UGNAME )
		if (!grp) then
			// Invalid group!
			window:Remove()
			return
		end
		
		// If it's an uneditable one, generate the filler
		if (grp.Hard) then
			plugs.Call( "CreateLabel2", "Manage Usergroup", 10, 10, window )
			plugs.Call( "CreateLabel2", "Usergroup Name: " .. window._UGNAME, 10, 30, window )
			plugs.Call( "CreateLabel2", "[ UNEDITABLE GROUP ]", 10, 50, window )
			local filler = vgui.Create( "DImage", window )
			filler:SetImage( "gmanage/ico_deny" )
			filler:SetImageColor( Color( 255, 255, 255, 100 ) )
			filler:SetSize( w*0.3, w*0.3 )
			filler:Center()
			return
		end
		
		// Create the sheet
		local sheet = vgui.Create( "DPropertySheet", window )
		sheet:SetPos( 5, 5 )
		sheet:SetSize( w-10, h-10 )
		
		// Get the list of tabs
		local tabs = plugs.MergeCallOverride( "GetGroupTabs" )
		
		// Cycle through adding and building each tab
		for k, v in pairs( tabs ) do
			local tab = plugs.Call( "AddPropertySheetTab", sheet, v[1], v[2] )
			plugs.Call( "BuildGroupTab", k, tab, grp )
		end
	end
	if (classname == "servermenu") then
		window:SetTitle( "Server Management" )
		local pn = vgui.Create( "DComboBox", window )
		pn:SetPos( 5, 30 )
		pn:SetSize( window:GetWide()*0.2, window:GetTall()-35 )
		pn:SetMultiple( false )
		for _, ac in pairs( plugs.MergeCall( "GetServerActions" ) ) do
			local item = pn:AddItem( ac[2] )
			item._AC = ac[1]
		end
		pn.SelectItem2 = pn.SelectItem
		function pn:SelectItem( item, onlyme )
			local ac = item._AC
			SELAC = ac
			if (ac) then plugs.Call( "BuildServerPanel", window, ac ) end
			self:SelectItem2( item, onlyme )
		end
	end
	if (classname == "limitsmenu") then
		window:SetTitle( "Server Limits" )
		local pn = vgui.Create( "DComboBox", window )
		pn:SetPos( 5, 30 )
		pn:SetSize( window:GetWide()*0.2, window:GetTall()-35 )
		pn:SetMultiple( false )
		for _, st in pairs( plugs.MergeCall( "GetLimitStructure" ) ) do
			if ((!st[3]) || (st[3]())) then
				local item = pn:AddItem( st[2] )
				item._SNAME = st[1]
			end
		end
		pn.SelectItem2 = pn.SelectItem
		function pn:SelectItem( item, onlyme )
			local st = item._SNAME
			if (st) then plugs.Call( "BuildLimitsPanel", window, st ) end
			self:SelectItem2( item, onlyme )
		end
	end
	if (classname == "limitscpanel") then
		local w = window:GetWide()
		local h = window:GetTall()
		local s = window._STRUCTNAME
		if (!s) then return end
		local lms = plugs.Call( "GetLimits", s )
		if (!lms) then return end
		local plst = vgui.Create( "DPanelList", window )
		plst:SetPos( w*0.1 + 10, 10 )
		plst:SetSize( w*0.9 - 20, h - 20 )
		plst:SetSpacing( 5 )
		plst:SetPadding( 5 )
		plst:EnableHorizontal( false )
		plst:EnableVerticalScrollbar( true )
		window._SLIDERS = {}
		for _, v in pairs( lms ) do
			local cvar = GetConVar( "sbox_max" .. v.var )
			if (cvar) then
				local sl = plugs.Call( "CreateLimitSlider", 0, 250, v.text )
				sl._LID = v.var
				sl:SetValue( cvar:GetInt() )
				plst:AddItem( sl )
				table.insert( window._SLIDERS, sl )
			end
		end
		local btn = vgui.Create( "DButton", window )
		btn:SetPos( 10, 10 )
		btn:SetSize( w*0.1 - 5, 20 )
		btn:SetText( "Apply" )
		btn._SLIDERS = window._SLIDERS
		function btn:DoClick()
			local tosend = {}
			local ch
			for _, sl in pairs( self._SLIDERS ) do
				if (sl && sl:IsValid()) then
					if (sl._CHANGED) then
						ch = true
						table.insert( tosend, { sl._LID, sl:GetValue() } )
					end
				end
			end
			datastream.StreamToServer( "gmanage_sv", { "limit_change", tosend } )
			if (self._LBL) then
				self._LBL:SetText( "Changes submitted!" )
				self._LBL:SizeToContents()
			end
		end
		local lbl = vgui.Create( "DLabel", window )
		lbl:SetPos( 10, 40 )
		lbl:SetText( "" )
		lbl:SizeToContents()
	end
end

local bgcol = Color( 100, 100, 100, 255 )
function PLUG:BuildPlayerPanel( window, ply )
	if (self.PlayerPanel) then
		self.PlayerPanel:Remove()
		self.PlayerPanel = nil
	end
	local pn = vgui.Create( "DPanel", window )
	pn:SetPos( window:GetWide()*0.2 + 10, 40 )
	pn:SetSize( window:GetWide()*0.8 - 20, window:GetTall() - 50 )
	function pn:Paint()
		draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), bgcol )
	end
	pn._PLAYER = ply
	self.PlayerPanel = pn
	self:BuildSubMenu( "playercpanel", pn )
end

function PLUG:BuildGroupPanel( window, ugname )
	if (self.GroupPanel) then
		self.GroupPanel:Remove()
		self.GroupPanel = nil
	end
	local pn = vgui.Create( "DPanel", window )
	pn:SetPos( window:GetWide()*0.2 + 10, 40 )
	pn:SetSize( window:GetWide()*0.8 - 20, window:GetTall() - 50 )
	function pn:Paint()
		draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), bgcol )
	end
	pn._UGNAME = ugname
	self.GroupPanel = pn
	self:BuildSubMenu( "groupcpanel", pn )
end

function PLUG:BuildNewGroupPanel( window )
	if (self.GroupPanel) then
		self.GroupPanel:Remove()
		self.GroupPanel = nil
	end
	local pn = vgui.Create( "DPanel", window )
	pn:SetPos( window:GetWide()*0.2 + 10, 40 )
	pn:SetSize( window:GetWide()*0.8 - 20, window:GetTall() - 50 )
	function pn:Paint()
		draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), bgcol )
	end
	self.GroupPanel = pn
	self:BuildSubMenu( "groupnewpanel", pn )
end

function PLUG:BuildLimitsPanel( window, structname )
	if (self.LimitsPanel) then
		self.LimitsPanel:Remove()
		self.LimitsPanel = nil
	end
	local pn = vgui.Create( "DPanel", window )
	pn:SetPos( window:GetWide()*0.2 + 10, 40 )
	pn:SetSize( window:GetWide()*0.8 - 20, window:GetTall() - 50 )
	function pn:Paint()
		draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), bgcol )
	end
	self.LimitsPanel = pn
	pn._STRUCTNAME = structname
	self:BuildSubMenu( "limitscpanel", pn )
end

function PLUG:BuildServerPanel( window, acname )
	if (self.ServerPanel) then
		self.ServerPanel:Remove()
		self.ServerPanel = nil
	end
	local pn = vgui.Create( "DPanel", window )
	pn:SetPos( window:GetWide()*0.2 + 10, 40 )
	pn:SetSize( window:GetWide()*0.8 - 20, window:GetTall() - 50 )
	function pn:Paint()
		draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), bgcol )
	end
	self.ServerPanel = pn
	plugs.Call( "BuildActionPanel", pn, acname )
end

function PLUG:BuildActionPanel( panel, action )
	if (action == "general") then
		local w = panel:GetWide()
		local h = panel:GetTall()
		
		plugs.Call( "CreateApplyEntry", "Server Name", GetConVar( "hostname" ):GetString(), 10, 10, w*0.35, w*0.1, 20, panel, function( self )
			local txt = self._TXT:GetValue()
			plugs.Call( "SendCommand", "sv_sethostname", txt )
		end )
		
		local defgrp = plugs.Call( "GetDefaultGroup" ) or ""
		
		plugs.Call( "CreateApplyEntry", "Default Group", defgrp, 10, 35, w*0.35, w*0.1, 20, panel, function( self )
			local txt = self._TXT:GetValue()
			plugs.Call( "SendCommand", "setdefgrp", txt )
		end )
		
		local pn = vgui.Create( "DPanel", panel )
		pn:SetPos( 5, 65 )
		pn:SetSize( w-10, 60 )
		local bgcol = Color( 50, 50, 50 )
		function pn:Paint()
			draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), bgcol )
		end
		
		local lbl = vgui.Create( "DLabel", pn )
		lbl:SetPos( 5, 5 )
		lbl:SetText( "Rcon:" )
		lbl:SizeToContents()
		
		local txt = vgui.Create( "DTextEntry", pn )
		txt:SetPos( 10 + lbl:GetWide(), 5 )
		txt:SetSize( pn:GetWide() - 15 - lbl:GetWide(), 20 )
		
		local btn = vgui.Create( "DButton", pn )
		btn:SetPos( 5, 30 )
		btn:SetSize( pn:GetWide()-10, 20 )
		btn:SetText( "Submit Command" )
		function btn:DoClick()
			plugs.Call( "SendCommand", "rcon", unpack( string.Explode( " ", txt:GetValue() ) ) )
		end
		
		return true
	end
	if (action == "gamemode") then
		local w = panel:GetWide()
		local h = panel:GetTall()
		local btn = vgui.Create( "DButton", panel )
		btn:SetPos( 10, 10 )
		btn:SetSize( w*0.5, 20 )
		btn:SetText( "Refresh Maps and Gamemodes" )
		local lbl = vgui.Create( "DLabel", panel )
		lbl:SetPos( w*0.5 + 20, 10 )
		lbl:SetText( "" )
		btn._LBL = lbl
		function btn:DoClick()
			plugs.Call( "SendCommand", "getgamemodes" )
			self._LBL:SetText( "Request sent!" )
			self._LBL:SizeToContents()
		end
		
		
		plugs.Call( "CreateLabel", "Gamemodes", 10 + (w*0.15), 35, panel )
		local pn = vgui.Create( "DComboBox", panel )
		pn:SetPos( 10, 55 )
		pn:SetSize( w*0.3, h-65 )
		pn:SetMultiple( false )
		local options = plugs.Call( "GetGamemodeList", id )
		local cnt = 0
		for _, v in pairs( options ) do
			local int, ext
			if (type( v ) == "table") then
				int = v[1]
				ext = v[2]
			else
				int = v
				ext = v
			end
			local item = pn:AddItem( ext )
			item._GMNAME = int
			cnt = cnt + 1
		end
		if (cnt == 0) then
			pn:AddItem( "No Gamemodes" )
		else
			pn.SelectItem2 = pn.SelectItem
			function pn:SelectItem( item, onlyme )
				local gm = item._GMNAME
				if (gm) then
					SELGM = gm
				end
				self:SelectItem2( item, onlyme )
			end
		end
		
		
		plugs.Call( "CreateLabel", "Maps", 20 + (w*0.45), 35, panel )
		local pn = vgui.Create( "DComboBox", panel )
		pn:SetPos( 20 + w*0.3, 55 )
		pn:SetSize( w*0.3, h-65 )
		pn:SetMultiple( false )
		local options = plugs.Call( "GetMapList", id )
		local cnt = 0
		for _, v in pairs( options ) do
			local int, ext
			if (type( v ) == "table") then
				int = v[1]
				ext = v[2]
			else
				int = v
				ext = v
			end
			local item = pn:AddItem( string.Replace( ext, ".bsp", "" ) )
			item._MAPNAME = string.Replace( int, ".bsp", "" )
			cnt = cnt + 1
		end
		if (cnt == 0) then
			pn:AddItem( "No Maps" )
		else
			pn.SelectItem2 = pn.SelectItem
			function pn:SelectItem( item, onlyme )
				local map = item._MAPNAME
				if (map) then
					SELMAP = map
				end
				self:SelectItem2( item, onlyme )
			end
		end
		
		
		plugs.Call( "CreateOptionButton", panel, w*0.6 + 30, 55, w*0.4 - 40, 20, "Change Map", plugs.MergeCall( "GetCountdownTimes" ), function( id )
			plugs.Call( "SendCommand", "changemap", SELGM, SELMAP, id )
		end )
		
		local btn = vgui.Create( "DButton", panel )
		btn:SetPos( w*0.6 + 30, 80 )
		btn:SetSize( w*0.4 - 40, 20 )
		btn:SetText( "Cancel Mapchange" )
		function btn:DoClick()
			plugs.Call( "SendCommand", "cancelchangemap" )
		end
		
		return true
	end
end

function PLUG:BuildRestrictPanel( window, classname, enttype )
	if (self.RestrictPanel) then
		self.RestrictPanel:Remove()
		self.RestrictPanel = nil
	end
	
	SELENTTYPE = enttype
	SELENTCLASS = classname
	
	local panel = vgui.Create( "DPanel", window )
	panel:SetPos( window:GetWide()*0.2 + 10, 40 )
	panel:SetSize( window:GetWide()*0.8 - 20, window:GetTall() - 50 )
	function panel:Paint()
		draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), bgcol )
	end
	self.RestrictPanel = panel
	local w = panel:GetWide()
	local h = panel:GetTall()
	
	local all = plugs.Call( "GetUGS" ) or {}
	local allowed = {}
	local disallowed = {}
	for _, v in pairs( all ) do
		v.Rstrict = v.Rstrict or {}
		if (table.HasValue( v.Rstrict, classname )) || (table.HasValue( v.Rstrict, "@ALL" )) then
			table.insert( disallowed, v )
		else
			table.insert( allowed, v )
		end
	end
	
	
	plugs.Call( "CreateLabel", "Allowed Access", w*0.54, 10, panel )
	local pn = vgui.Create( "DComboBox", panel )
	pn:SetPos( w*0.38, 30 )
	pn:SetSize( w*0.3, panel:GetTall()-40 )
	pn:SetMultiple( false )
	pn._CLASSNAME = classname
	pn._ENTTYPE = enttype
	local cnt = 0
	for _, v in pairs( allowed ) do
		cnt = cnt + 1
		local item = pn:AddItem( v.PrintName )
		item._UGNAME = v.ClassName
	end
	if (cnt == 0) then
		// pn:AddItem( "No Actions" )
	else
		pn.SelectItem2 = pn.SelectItem
		function pn:SelectItem( item, onlyme )
			local ug = item._UGNAME
			if (ug) then
				plugs.Call( "SendCommand", "grp_restrict", ug, self._ENTTYPE, self._CLASSNAME )
			end
			self:SelectItem2( item, onlyme )
		end
	end
	
	
	plugs.Call( "CreateLabel", "Unallowed Access", w*0.845, 10, panel )
	local pn = vgui.Create( "DComboBox", panel )
	pn:SetPos( w*0.69, 30 )
	pn:SetSize( w*0.3, panel:GetTall()-40 )
	pn:SetMultiple( false )
	pn._CLASSNAME = classname
	pn._ENTTYPE = enttype
	local cnt = 0
	for _, v in pairs( disallowed ) do
		cnt = cnt + 1
		local item = pn:AddItem( v.PrintName )
		item._UGNAME = v.ClassName
	end
	if (cnt == 0) then
		// pn:AddItem( "No Actions" )
	else
		pn.SelectItem2 = pn.SelectItem
		function pn:SelectItem( item, onlyme )
			local ug = item._UGNAME
			if (ug) then
				plugs.Call( "SendCommand", "grp_unrestrict", ug, self._ENTTYPE, self._CLASSNAME )
			end
			self:SelectItem2( item, onlyme )
		end
	end
end

function PLUG:DataStream( tbl )
	if (tbl[1] == "ugs") || (tbl[1] == "ugdat") then
		if (plugs.Call( "GetCurrentSubMenuClass" ) == "groupmenu") then
			local menu = plugs.Call( "OpenSubMenu", "groupmenu" )
			if (SELGRP) then
				timer.Simple( 0.5, plugs.Call, "BuildGroupPanel", menu, SELGRP )
			end
		end
		if (plugs.Call( "GetCurrentSubMenuClass" ) == "restrictmenu") then
			local menu = plugs.Call( "OpenSubMenu", "restrictmenu" )
			if (SELENTTYPE && SELENTCLASS) then
				plugs.Call( "BuildRestrictPanel", menu, SELENTCLASS, SELENTTYPE )
			end
		end		
	end
	if (tbl[1] == "gmdata") then
		if (plugs.Call( "GetCurrentSubMenuClass" ) == "servermenu") then
			local menu = plugs.Call( "OpenSubMenu", "servermenu" )
			if (SELAC) then
				plugs.Call( "BuildServerPanel", menu, SELAC )
			end
		end
	end
end

function PLUG:DoCustomKick( ply )
	local win = vgui.Create( "DFrame" )
	win:SetSize( ScrW()*0.5, ScrH()*0.25 )
	win:Center()
	win:SetTitle( "GManage - Kick Player" )
	win:SetDraggable( true )
	win:ShowCloseButton( true )
	
	function win:Paint()
		local w, h = self:GetWide(), self:GetTall()
		surface.SetDrawColor( color_black )
		surface.DrawRect( 0, 0, w, h )
		surface.SetDrawColor( color_white )
		surface.DrawOutlinedRect( 0, 0, w, h )
		surface.DrawLine( 0, 25, w, 25 )
	end
	
	local txt = vgui.Create( "DTextEntry", win )
	txt:SetPos( 5, 30 )
	txt:SetSize( win:GetWide()-10, 20 )
	
	local btn = vgui.Create( "DImageButton", win )
	btn:SetImage( "gmanage/ico_accept" )
	btn:SetSize( 64, 64 )
	btn:SetPos( 0, 55 )
	btn:CenterHorizontal()
	function btn:DoClick()
		win:Remove()
		local reason = txt:GetValue()
		plugs.Call( "RunPlayerOperation", "dokick", ply, reason )
	end
	
	win:SetTall( 128 )
	win:MakePopup()
end